package com.higgins.eventtracking.server.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.Query;

import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.finance.FixedOutgoingDTO;
import com.higgins.eventtracking.client.finance.OutstandingTransactionDTO;
import com.higgins.eventtracking.client.rpc.FixedOutgoingService;
import com.higgins.eventtracking.server.GenericDataStoreService;
import com.higgins.eventtracking.server.dataModel.Asset;
import com.higgins.eventtracking.server.dataModel.FixedOutgoing;
import com.higgins.eventtracking.server.dataModel.OutstandingTransaction;
import com.higgins.eventtracking.server.dataModel.Transaction;
import com.higgins.eventtracking.server.projectmanagement.ProjectItem;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class FixedOutgoingServiceImpl extends GenericDataStoreService implements
		FixedOutgoingService {
	private static final Logger log = Logger.getLogger(FixedOutgoingServiceImpl.class.getName());


	@Override
	public List<FixedOutgoingDTO> retrieveFixedOutgoings()
			throws IllegalArgumentException, SecurityAccessDeniedException {
		this.checkAuthority();
		openPersistenceManager();
		Query query=pm.newQuery(FixedOutgoing.class);
		query.setOrdering("dayOfMth asc");
		@SuppressWarnings("unchecked")
		List<FixedOutgoing> results = (List<FixedOutgoing>)query.execute();
			
		Double total = 0.0;
		List<FixedOutgoingDTO> list = new ArrayList<FixedOutgoingDTO>();
		for (FixedOutgoing fixedOutgoing:results)	{
			FixedOutgoingDTO fixedOutgoingDTO = fixedOutgoing.toDTO();			
			total = total + fixedOutgoingDTO.getAmount();
			fixedOutgoingDTO.setTotal(total);
			list.add(fixedOutgoingDTO);
		}
		closePersistenceManager();
		return list;
	}

	@Override
	public String updateFixedOutgoing(FixedOutgoingDTO dto)
			throws IllegalArgumentException, SecurityAccessDeniedException {
		openPersistenceManager();
		log.info("updateFixedOutgoing()");
		this.checkAuthority();
		FixedOutgoing fixedOutgoing;
		if (dto.getId()!=null)	{
			fixedOutgoing = pm.getObjectById(FixedOutgoing.class, dto.getId());
			fixedOutgoing.update(dto);
		}
		else	{ 
			fixedOutgoing = new FixedOutgoing(dto);
		}
		pm.makePersistent(fixedOutgoing);
		closePersistenceManager();
		return fixedOutgoing.getId().toString();
	}

	@Override
	public List<OutstandingTransactionDTO> retrieveOutstandingTransactions()
			throws IllegalArgumentException, SecurityAccessDeniedException {
		// TODO Auto-generated method stub
		this.checkAuthority();
		openPersistenceManager();
		Query query=pm.newQuery(OutstandingTransaction.class);
		query.setOrdering("timestamp asc");
		@SuppressWarnings("unchecked")
		List<OutstandingTransaction> results = (List<OutstandingTransaction>)query.execute();
			
		Double total = 0.0;
		List<OutstandingTransactionDTO> list = new ArrayList<OutstandingTransactionDTO>();
		for (OutstandingTransaction outstandingTransaction:results)	{
			OutstandingTransactionDTO outstandingTransactionDTO = outstandingTransaction.toDTO();			
			total = total + outstandingTransactionDTO.getAmount();
			outstandingTransactionDTO.setTotal(total);
			list.add(outstandingTransactionDTO);
		}
		closePersistenceManager();
		return list;
	}

	@Override
	public String updateOutstandingTransaction(OutstandingTransactionDTO dto)
			throws IllegalArgumentException, SecurityAccessDeniedException {
		openPersistenceManager();
		log.info("updateOutstandingTransaction()");
		this.checkAuthority();
		OutstandingTransaction outstandingTransaction;
		if (dto.getId()!=null)	{
			outstandingTransaction = pm.getObjectById(OutstandingTransaction.class, dto.getId());
			outstandingTransaction.update(dto);
		}
		else	{ 
			outstandingTransaction = new OutstandingTransaction(dto);
		}
		pm.makePersistent(outstandingTransaction);
		closePersistenceManager();
		return outstandingTransaction.getId().toString();
	}
	
	@Override
	public String executeOutstandingTransaction(OutstandingTransactionDTO dto)	
			throws IllegalArgumentException, SecurityAccessDeniedException	{
		openPersistenceManager();
		log.info("updateOutstandingTransaction()");
		this.checkAuthority();
		OutstandingTransaction outstandingTransaction;
		if (dto.getId()!=null)	{
			outstandingTransaction = pm.getObjectById(OutstandingTransaction.class, dto.getId());
		}
		else	{ 
			outstandingTransaction = new OutstandingTransaction(dto);
		}
		Transaction transaction = new Transaction();
		transaction.setTimestamp(new Date());
		transaction.setAmount(outstandingTransaction.getAmount());
		transaction.setTransactionType(outstandingTransaction.getTransactionType());
		transaction.setDescription(outstandingTransaction.getDescription());
		pm.makePersistent(transaction);
		
		pm.deletePersistent(outstandingTransaction);
		
		closePersistenceManager();
		return outstandingTransaction.getId().toString();
	}

	@Override
	public String deleteOutstandingTransaction(Long id)
			throws IllegalArgumentException, SecurityAccessDeniedException {
		String returnString = null;
		openPersistenceManager();
		log.info("updateOutstandingTransaction()");
		this.checkAuthority();
		OutstandingTransaction outstandingTransaction;
		if (id!=null)	{
			outstandingTransaction = pm.getObjectById(OutstandingTransaction.class, id);
			pm.deletePersistent(outstandingTransaction);
			returnString = outstandingTransaction.getId().toString();
		}
		
		closePersistenceManager();
		return returnString;
	}
	
}
